//
//  NSString+XYAdd.m
//  PeiJue
//
//  Created by lxy on 2017/9/26.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "NSString+XYAdd.h"

@implementation NSString (XYAdd)

- (CGSize)xy_sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    NSMutableDictionary *attr = [NSMutableDictionary new];
    attr[NSFontAttributeName] = font;
    if (lineBreakMode != NSLineBreakByWordWrapping) {
        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        paragraphStyle.lineBreakMode = lineBreakMode;
        attr[NSParagraphStyleAttributeName] = paragraphStyle;
    }
    CGRect rect = [self boundingRectWithSize:size
                                     options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                  attributes:attr context:nil];
    return rect.size;
}



- (CGFloat)xy_widthForFont:(UIFont *)font {
    CGSize size = [self xy_sizeForFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.width;
}


- (CGFloat)xy_heightForFont:(UIFont *)font width:(CGFloat)width {
    CGSize size = [self xy_sizeForFont:font size:CGSizeMake(width, HUGE) mode:NSLineBreakByWordWrapping];
    return size.height;
}


@end
